package view.Core;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.*;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONException;

public class registionTreeActivity extends Activity {
	private EditText editTree, editQRcode;
	private ImageButton id_mapping_imageButton1;
	private Button registionbutton;
	private TextView textViewResult;
	boolean status;

	private static final int REQUEST_CODE = 3434;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.id_mapping);

		findViewById();

		// textViewResult = (TextView)findViewById(R.id.textViewResult);

		// ///onClickListener//
		registionbutton.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				mapping();
			}

		}

		);// /onClickListener//

		// ///onClickListener//
		id_mapping_imageButton1
				.setOnClickListener(new Button.OnClickListener() {
					@Override
					public void onClick(View v) {
						//if((editTree.getText().toString()).!equals("null") && editQRcode.getText().toString())
						scanQRcode();
						
						/*
						String status = selectAllByID("qrcode", "QRcode",
								editQRcode.getText().toString(), "status");
						if (status.equals(null) || status.equals("")
								|| status.equals("used"))
							toastMsg("QR Code has beend used!");
						else{
							toastMsg(status);
							scanQRcode();
							}
							*/
					}

				}

				);// /onClickListener//
	}

	public void scanQRcode() {
		Intent intent = new Intent();
		Intent i = new Intent(this, encodeActivitySub.class);
		startActivityForResult(i, REQUEST_CODE);
	}

	public void setStatus() {

	}

	public String selectAllByID(String inputTable, String inputField,
			String inputID, String returnField) {
		String field = "";
		String reply;

		try {
			String url = getResources().getString(R.string.serverURL)
					+ "getAllByID.php?inputTable=" + inputTable
					+ "&inputField=" + inputField + "&inputID=" + inputID;
			// getJsonByTreeID.php
			HttpClient client = new DefaultHttpClient();
			HttpGet request = new HttpGet(url);
			HttpResponse response = client.execute(request);

			String line = "";
			reply = "";
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					response.getEntity().getContent()));
			while ((line = reader.readLine()) != null) {
				reply = line;
			}
			reader.close();
			try {

				JSONArray json = new JSONArray(reply);

				String getField = json.getString(0);

				JSONObject jsonField = new JSONObject(getField);

				field = jsonField.getString(returnField);

			} catch (JSONException e) {
				toastMsg("" + e);
			}
		} catch (Exception e) {
			toastMsg("" + e);
		}

		return field;
	}

	public void mapping() {

		try {

			String inputTreeID = "inputtreeID=" + editTree.getText().toString();
			String inputQr = "&inputQrCode=" + editQRcode.getText().toString();
			String url = getResources().getString(R.string.serverURL)
					+ getResources().getString(R.string.registionPhp)
					+ inputTreeID + inputQr;

			HttpClient client = new DefaultHttpClient();
			HttpGet request = new HttpGet(url);
			HttpResponse response = client.execute(request);

			String line = "";
			String reply = "";

			BufferedReader reader = new BufferedReader(new InputStreamReader(
					response.getEntity().getContent()));
			while ((line = reader.readLine()) != null) {
				reply += line;
			}
			reader.close();
			try {

				toastMsg("Tree " + editTree.getText().toString() + '\n'
						+ "Id Mapp success.");

			} catch (Exception e) {
				toastMsg("Input Error! Please Check input is Correct" + '\n'
						+ e);
			}
		} catch (Exception e) {
			toastMsg("Please Check network connection" + '\n' + e);
		}
	}

	public void toastMsg(String msg) {
		Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
	}

	public void findViewById() {
		editTree = (EditText) findViewById(R.id.id_mapping_editText1);
		editQRcode = (EditText) findViewById(R.id.id_mapping_editText2);
		registionbutton = (Button) findViewById(R.id.id_mapping_button1);
		id_mapping_imageButton1 = (ImageButton) findViewById(R.id.id_mapping_imageButton1);

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
			if (data.hasExtra("decodeString")) {
				String decodeString = data.getExtras()
						.getString("decodeString");
				editQRcode.setText(decodeString);
				toastMsg("Scan success. " + '\n' + "The QrCode is "
						+ decodeString);

			}
		}
	}
}